package com.wetech.demo.web3j.controller;
import com.wetech.demo.web3j.service.LXLTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
@Slf4j
@RestController
@RequestMapping("/api/token")
@RequiredArgsConstructor
public class LXLTokenController {
    @Autowired
    private LXLTokenService tokenService;
    /**
     * Deploy a new LXLToken contract
     * @return the address of the deployed contract
     */
    @PostMapping("/deploy")
    public CompletableFuture<ResponseEntity<Map<String, String>>> deployContract() {
        return tokenService.deployContract()
                .thenApply(address -> {
                    Map<String, String> response = new HashMap<>();
                    response.put("contractAddress", address);
                    return ResponseEntity.ok(response);
                });
    }
    /**
     * Load an existing contract
     * @param address the address of the contract to load
     * @return a success message
     */
    @PostMapping("/load")
    public ResponseEntity<Map<String, String>> loadContract(@RequestParam String address) {
        tokenService.loadContract(address);
        Map<String, String> response = new HashMap<>();
        response.put("message", "Contract loaded successfully");
        response.put("contractAddress", address);
        return ResponseEntity.ok(response);
    }
    /**
     * 铸币接口 mint
     * @param body 前端传入 {"amount": "1000"}
     */
    @PostMapping("/mint")
    public CompletableFuture<ResponseEntity<Map<String, String>>> mint(@RequestBody Map<String, String> body) {
        BigInteger amount = new BigInteger(body.get("amount"));
        return tokenService.mint(amount)
                .thenApply(txHash -> {
                    Map<String, String> res = new HashMap<>();
                    res.put("amount", amount.toString());
                    res.put("txHash", txHash);
                    res.put("status", "mint success");
                    return ResponseEntity.ok(res);
                });
    }
    // 转账接口
    @PostMapping("/transfer")
    public CompletableFuture<ResponseEntity<Map<String, String>>> transfer(@RequestBody Map<String, String> body) {
        String to = body.get("to");
        BigInteger amount = new BigInteger(body.get("amount"));
        return tokenService.transfer(to, amount)
                .thenApply(txHash -> {
                    Map<String, String> response = new HashMap<>();
                    response.put("to", to);
                    response.put("amount", amount.toString());
                    response.put("txHash", txHash);
                    response.put("status", "success");
                    return ResponseEntity.ok(response);
                });
    }
    /**
     * 查询余额 balanceOf(address)
     */
    @GetMapping("/balance")
    public CompletableFuture<ResponseEntity<Map<String, String>>> getBalance(@RequestParam String address) {
        return tokenService.getBalance(address)
                .thenApply(balance -> {
                    Map<String, String> res = new HashMap<>();
                    res.put("address", address);
                    res.put("balance", balance.toString());
                    return ResponseEntity.ok(res);
                });
    }
    /**
     * 授权 approve(spender, value)
     */
    @PostMapping("/approve")
    public CompletableFuture<ResponseEntity<Map<String, String>>> approve(@RequestBody Map<String, String> body) {
        String spender = body.get("spender");
        BigInteger value = new BigInteger(body.get("value"));
        return tokenService.approve(spender, value)
                .thenApply(txHash -> {
                    Map<String, String> res = new HashMap<>();
                    res.put("spender", spender);
                    res.put("value", value.toString());
                    res.put("txHash", txHash);
                    res.put("status", "approve success");
                    return ResponseEntity.ok(res);
                });
    }
    /**
     * 授权转账 C->A->B,C使用A的代币转账给B（前提是A授权给C代币）transferFrom(from, to, value)
     */
    @PostMapping("/transferFrom")
    public CompletableFuture<ResponseEntity<Map<String, String>>> transferFrom(@RequestBody Map<String, String> body) {
        String from = body.get("from");
        String to = body.get("to");
        BigInteger value = new BigInteger(body.get("value"));
        return tokenService.transferFrom(from, to, value)
                .thenApply(txHash -> {
                    Map<String, String> res = new HashMap<>();
                    res.put("from", from);
                    res.put("to", to);
                    res.put("value", value.toString());
                    res.put("txHash", txHash);
                    res.put("status", "transferFrom success");
                    return ResponseEntity.ok(res);
                });
    }
    @GetMapping("/address")
    public ResponseEntity<Map<String, String>> getContractAddress() {
        String address = tokenService.getContractAddress();
        Map<String, String> response = new HashMap<>();
        if (address != null) {
            response.put("contractAddress", address);
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "No contract loaded");
            return ResponseEntity.ok(response);
        }
    }
}
